/**
 * 
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;

import logger.PoolLogger;
import dbmanager.DBManager;

/**
 * @author Divyesh
 *
 */
public class FileWriter 
{
	FileLock lock;
	String Path;
	String mode;
	public RandomAccessFile file;
	PoolLogger pl;
	
	public FileWriter(String path, String mode) throws FileNotFoundException  
	{
		this.Path = path;
		this.mode = mode;
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
		
	public RandomAccessFile fileOpen(boolean force)
	{	
		try 
		{
			while (true)
			{
				//Filepermission.setReadWrite(Path);
				
				//while (channel.isOpen() == false);
				try 
				{
					file = new RandomAccessFile(Path, mode);
					FileChannel channel = file.getChannel();
					channel.force(force);
					lock = channel.lock();
					break;
				}
				catch (NonWritableChannelException e1) 
				{
					System.out.println("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.info("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.debug("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.warn("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.fatal("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.error("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.trace("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					//e1.printStackTrace();
				}
				catch (Exception e) 
				{
					System.out.println("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.info("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.debug("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.warn("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.fatal("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.error("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					pl.trace("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
					
					//e.printStackTrace();
					Thread.sleep(2000);
				}
			}			
		} 
		catch (Exception e) 
		{
			System.out.println("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.info("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.debug("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.warn("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.fatal("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.error("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			pl.trace("file:FileWriter:fileOpen(force):Exception::"+e.getMessage());
			
			//e.printStackTrace();
		}
		return file;
	}
	public RandomAccessFile fileOpen()
	{
		try 
		{
			while (true)
			{
				//Filepermission.setReadWrite(Path);
				file = new RandomAccessFile(Path, mode);
				//while (channel.isOpen() == false);
				try 
				{
					FileChannel channel = file.getChannel();
					lock = channel.lock();
					break;
				}
				catch (NonWritableChannelException e1) 
				{
					System.out.println("file:FileWriter:fileOpen():Exception::"+e1.getMessage());
					pl.info("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.debug("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.warn("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.fatal("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.error("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					pl.trace("file:FileWriter:fileOpen(force):Exception::"+e1.getMessage());
					
					//e1.printStackTrace();
				}
				catch (Exception e) 
				{
					System.out.println("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.info("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.debug("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.warn("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.fatal("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.error("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					pl.trace("file:FileWriter:fileOpen():Exception::"+e.getMessage());
					
					//e.printStackTrace();
					Thread.sleep(2000);
				}
			}			
		} 
		catch (Exception e) 
		{
			System.out.println("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.info("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.debug("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.warn("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.fatal("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.error("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.trace("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			
			//e.printStackTrace();
		}
		return file;
	}

	public void fileClose()
	{
		try 
		{
			try 
			{
				lock.release();
			} catch (Exception e) {
			}
			file.close();
			//Filepermission.setReadOnly(Path);
		} 
		catch (IOException e) {
			System.out.println("file:FileWriter:fileClose():Exception::"+e.getMessage());
			pl.info("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.debug("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.warn("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.fatal("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.error("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			pl.trace("file:FileWriter:fileOpen():Exception::"+e.getMessage());
			
		}
	}	
}
